<?php

namespace App\Http\Models;

use Illuminate\Support\Facades\Redis;

use App\Http\Models\MarketTypeModel;
use App\Http\Models\GameDataModel;
use App\Http\Models\CrowdFundingModel;
use App\Http\Models\VotingDataModel;
use App\Http\Models\VotingLogModel;
use App\Http\Models\QuestionScoreModel;
use App\Http\Models\LotteryModel;
use App\Http\Models\LotteryLogModel;
use App\Http\Models\LotteryPrizeModel;

use App\Http\Models\GuessscoreJoinModel;
use App\Http\Models\GuessscoreActionModel;
use App\Http\Models\LotteryTimesModel;
use App\Http\Models\CollectModel;
use App\Http\Models\CollectLogModel;

class ClearModel
{
    /**
     * 清除相关数据
     */
    public static function clear_raffle( $appid)
    {
        self::empty_game_data( $appid);
        self::empty_rank($appid);
        self::reset_game_code($appid);
        self::empty_question_score_data($appid);

        $redis = Redis::connection('db_main');
        $redis->del('appid:config:raffle:'. $appid);
        $redis->del('appid:config:exchange:'. $appid);
        $redis->del('appid:config:signedraffle:'. $appid);
        $redis->del('appid:config:signedexchange:'. $appid);
    }
    /**
     * 清除相关数据
     */    
    public static function clear_rank( $appid)
    {
        self::empty_rank($appid);
    }
    /**
     * 清除相关数据
     */    
    public static function clear_form( $appid)
    {
        self::empty_game_data($appid);

        $redis = Redis::connection('db_main');
        $redis->del('appid:config:entry:'. $appid);
    }
    /**
     * 清除相关数据
     */    
    public static function clear_crowd( $appid)
    {
        self::empty_game_data($appid);
        self::reset_game_code($appid);
        self::empty_crowd($appid);

        $redis = Redis::connection('db_main');
        $redis->del('appid:config:raffle:'. $appid);
    }
    /**
     * 清除相关数据
     */    
    public static function clear_crowd_raffle( $appid)
    {
        self::empty_game_data($appid);
        self::reset_game_code($appid);
        self::empty_crowd($appid);

        $redis = Redis::connection('db_main');
        $redis->del('appid:config:raffle:'. $appid);
        $redis->del('appid:config:exchange:'. $appid);
    }
    /**
     * 清除相关数据
     */    
    public static function clear_vote( $appid)
    {
        self::empty_vote_data($appid);

        $redis = Redis::connection('db_main');
        $redis->set('vote:'.$appid,"0");
    }

    /**
     * 清除相关数据
     */    
    public static function clear_duobao( $appid)
    {

    }

    /**
     * 清除 Game data 表
     */
    private static function empty_game_data( $appid )
    {
        return GameDataModel::where(["appid" => $appid, "status" => GameDataModel::STATUS_ACTIVE]) ->update(["status" => GameDataModel::STATUS_DELETE]);
    }

    /**
     * 清除 rank 数据
     */
    private static function empty_rank( $appid )
    {
        $redis = Redis::connection('db_main');
        $key = 'rank:appid:'. $appid .':lot_id:0';
        $redis->del($key);

        $redis->del('appid:config:rank:'. $appid);
    }

    /**
     * 清除 众筹 数据
     */
    public static function empty_crowd( $appid )
    {
        CrowdFundingModel::where(["appid" => $appid, "status" => CrowdFundingModel::STATUS_ACTIVE]) -> update(["status" => CrowdFundingModel::STATUS_DELETE]);

        $redis = Redis::connection('db_main');
        $fields = $redis->hkeys('appid:'. $appid);
        if($fields && is_array($fields) && count($fields)>0) {
            foreach ($fields as $field) {
                if(false !== strpos($field, 'crowd:curTotalPay:') || false !== strpos($field, 'crowd:curTime:') || false !== strpos($field, 'crowd:isPlayed:')) {
                    $redis->hdel('appid:'. $appid, $field);
                }
            }
        }

    }

    /**
     * 清空刮菜的列表数据（清空关系表记录）
     */
    public static function empty_relation_records($appid)
    {
        return CrowdFundingModel::where(["appid" => $appid, "status" => CrowdFundingModel::STATUS_ACTIVE]) -> update(["status" => CrowdFundingModel::STATUS_DELETE]);
    }

    /**
     * 清空投票模板数据
     */
    private static function empty_vote($appid) {
        return VoteModel::where(["appid" => $appid, "status" => VoteModel::STATUS_ACTIVE]) -> update(["status" => VoteModel::STATUS_DELETE]);
    }

   /**
     * 请求分数匹配数据
     */
    private static function empty_question_score_data($appid) {
        return QuestionScoreModel::where(["appid" => $appid, "status" => QuestionScoreModel::STATUS_ACTIVE]) -> update(["status" => QuestionScoreModel::STATUS_DELETE]);
    }

    /**
     * 清空投票数据
     */
    private static function empty_vote_data($appid) {
        VotingDataModel::where(["appid" => $appid, "status" => VotingDataModel::STATUS_ACTIVE]) -> update(["status" => VotingDataModel::STATUS_DELETE]);
        VotingLogModel::where(["appid" => $appid, "status" => VotingLogModel::STATUS_ACTIVE]) -> update(["status" => VotingLogModel::STATUS_DELETE]);
    }



    /**
     * 清空夺宝数据
     */
    private static function empty_duobao($appid) {
        // 夺宝清空数据功能暂时欠缺
    }

    /*
     * 清空访问日志
     */
    private static function empty_stat_log($appid) {
        // 不需要了
    }



    /*
     * 清空中奖数据
     */
    public static function empty_lottery($appid) {
        return LotteryModel::where(["appid" => $appid, "status" => LotteryModel::STATUS_ACTIVE])
            ->where('lot_type', '!=', MarketTypeModel::TYPE_REDPACK)
            ->update(["status" => LotteryModel::STATUS_DELETE]);

    }

    /*
     * 清空摇奖/抽奖日志
     */
    public static function empty_lottery_log($appid) {
        return LotteryLogModel::where(["appid" => $appid, "status" => LotteryLogModel::STATUS_ACTIVE])
            ->where('lot_type', '!=', MarketTypeModel::TYPE_REDPACK)
            ->update(["status" => LotteryLogModel::STATUS_DELETE]);
    }

    /**
     * 还原奖品设置
     */
    public static function reset_lottery_prize($appid) {
        //获取奖品信息
        return LotteryPrizeModel::where(["appid" => $appid, "status" => LotteryPrizeModel::STATUS_ACTIVE])
            ->where('lot_type', '!=', MarketTypeModel::TYPE_REDPACK)
            ->update(["status" => LotteryPrizeModel::STATUS_DELETE]);
    }
    /*
     * 重置礼券状态，项目为奖券/礼券/优惠券、众筹时，复原所有奖券
     * 注：将status为1、3的记录status置为2，并将openid置空
    */
    private static function reset_game_code($appid) {
        return GameCodeModel::where(["appid" => $appid])
            ->whereNotIn('status', [GameCodeModel::STATUS_EXCHANGED, GameCodeModel::STATUS_RECEIVE])
            ->update(["status" => GameCodeModel::STATUS_UNEXCHANGE]);
    }

    /**
     * iscollect2
     */
    public function empty_collect2($appid, $key = '')
    {
        CollectModel::where(["appid" => $appid, "status" => CollectModel::STATUS_ACTIVE]) ->update(["status" => CollectModel::STATUS_DELETE]);
        CollectLogModel::where(["appid" => $appid, "status" => CollectLogModel::STATUS_ACTIVE]) ->update(["status" => CollectLogModel::STATUS_DELETE]);

        if($key)
        {
            $redis = Redis::connection('db_main');
            $redis->set($key, 0);
        }
    }

    /**
     * iscollect3
     */
    public function empty_collect3( $appid)
    {
        LotteryTimesModel::where(["appid" => $appid, "status" => LotteryTimesModel::STATUS_ACTIVE]) ->update(["status" => LotteryTimesModel::STATUS_DELETE]);
        GuessscoreJoinModel::where(["appid" => $appid]) 
            ->where("status", '!=', GuessscoreJoinModel::STATUS_DELETE)
            ->update(["status" => GuessscoreJoinModel::STATUS_DELETE]);

        GuessscoreActionModel::where(["appid" => $appid]) 
            ->where("status", '!=', GuessscoreActionModel::STATUS_DELETE)
            ->update(["status" => GuessscoreActionModel::STATUS_DELETE]);
    }

    /**
     * 清空项目缓存
     * @param $appid
     * @param $token
     */
    public static function clear_project($appid, $token){
        self::empty_game_data($appid);
        self::reset_game_code($appid);
        self::empty_crowd($appid);

        $redis = Redis::connection('db_main');
        if(!empty($id)) $redis->del('project:'. $appid);
        if(!empty($token)) $redis->del('project:'. $appid);
    }



}
